import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
} from 'typeorm';
import { UserGender, UserStatus } from '../users.enum';
import { Exclude, Expose } from 'class-transformer';

@Entity({ name: 'tb_users' })
export class User {
  @PrimaryGeneratedColumn('uuid', {
    name: 'user_id',
    comment: '用户ID 主键',
    primaryKeyConstraintName: 'user_id',
  })
  userId: string;

  @Column({
    name: 'username',
    type: 'varchar',
    length: 255,
    comment: '用户名',
  })
  username: string;

  @Column({
    name: 'nickname',
    type: 'varchar',
    length: 255,
    nullable: true,
    comment: '用户昵称',
  })
  nickname: string;

  @Column({
    name: 'password',
    type: 'varchar',
    length: 255,
    comment: '用户密码，以hash+盐存储，确保安全性',
  })
  @Exclude()
  password: string;

  @Expose()
  get hello() {
    return 'hello world';
  }

  @Column({
    name: 'avatar',
    type: 'varchar',
    length: 255,
    nullable: true,
    comment: '用户头像',
  })
  avatar: string;

  @Column({
    name: 'email',
    type: 'varchar',
    length: 255,
    unique: true,
    nullable: true,
    comment: '唯一，用于找回密码、通知等',
  })
  email: string;

  @Column({
    name: 'phone',
    type: 'varchar',
    length: 255,
    unique: true,
    nullable: true,
    comment: '唯一，用于绑定和验证',
  })
  phone: string;

  @Column({
    name: 'gender',
    type: 'enum',
    enum: UserGender,
    default: UserGender.UNKNOWN,
    comment: '性别，枚举，MALE-男，FEMALE-女，UNKNOWN-未知',
  })
  gender: string;

  @Column({
    name: 'birthday',
    type: 'date',
    comment: '生日，格式为YYYY-MM-DD',
    nullable: true,
  })
  birthday: Date | null;

  @Column({
    name: 'qq_openid',
    type: 'varchar',
    length: 255,
    unique: true,
    nullable: true,
    comment: 'QQ，唯一标识openid',
  })
  qqOpenid: string;

  @Column({
    name: 'weibo_openid',
    type: 'varchar',
    length: 255,
    unique: true,
    nullable: true,
    comment: '微博，唯一标识openid',
  })
  weiboOpenid: string;

  @Column({
    name: 'weixin_openid',
    type: 'varchar',
    length: 255,
    unique: true,
    nullable: true,
    comment: '微信，唯一标识openid',
  })
  weixin_openid: string;

  @Column({
    name: 'status',
    type: 'enum',
    enum: UserStatus,
    default: UserStatus.NORMAL,
    comment:
      '帐号状态，NORMAL-正常，ABNORMAL-异常，LOCKED-已锁定，EXPIRED-已过期，DELETED-已删除，CANCELLED-已注销',
  })
  status: string;

  @UpdateDateColumn({
    name: 'last_login_at',
    type: 'timestamp',
    comment: '上一次登录时间',
  })
  lastLoginAt: number;

  @UpdateDateColumn({
    name: 'updated_at',
    type: 'timestamp',
    comment: '最后一次更新时间',
  })
  updatedAt: number;

  @CreateDateColumn({
    name: 'created_at',
    type: 'timestamp',
    comment: '创建时间',
  })
  createdAt: number;

  constructor(partial: Partial<User>) {
    Object.assign(this, partial);
  }
}
